package com.springboot.project.common.controller;


import com.springboot.config.annotion.BusinessLog;
import com.springboot.config.tool.Tip;
import com.springboot.project.common.entity.Module;
import com.springboot.project.common.node.ZTreeNode;
import com.springboot.project.common.service.impl.ModuleServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author lby
 * @since 2018-05-06
 */
@Controller
@RequestMapping("/module")
public class ModuleController {

    private static String PREFIX = "system/menu/";

    @Autowired
    ModuleServiceImpl moduleService;

    /**
     * 跳转到查看管理员列表的页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "menu";
    }

    /**
     * 角色权限树
     */
    @RequestMapping(value = "/menuTreeListByRoleId/{roleId}")
    @ResponseBody
    public List<ZTreeNode> menuTreeListByRoleId(@PathVariable Integer roleId) {
        List<Integer> menuIds = moduleService.getMenuIdsByRoleId(roleId);
        if(menuIds.size() == 0)
        {
            List<ZTreeNode> roleTreeList = moduleService.menuTreeList();
            return roleTreeList;
        }
        else {
            List<ZTreeNode> roleTreeListByUserId = moduleService.menuTreeListByMenuIds(menuIds);
            return roleTreeListByUserId;
        }
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    List<Module> list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        List<Module> moduleList = moduleService.list(params);
        return moduleList;
    }

    /**
     * 跳转到菜单新增画面
     */
    @RequestMapping("/add")
    public String addView() {
        return PREFIX + "menu_add";
    }

    /**
     * 跳转到编辑管理员页面
     */
    @RequestMapping("/edit/{id}")
    public String editView(@PathVariable Integer id, Model model) {

        Module menu = moduleService.selectById(id);
        model.addAttribute("menu",menu);
        //上级菜单
        Map<String, Object> params = new HashMap<String, Object>();
        String pCode = menu.getParentcode();
        params.put("code", pCode);
        if(pCode.equals("0"))
        {
            model.addAttribute("pcodeName", "顶级");
        }else {
            List<Module> moduleList = moduleService.list(params);
            if (moduleList.size() > 0) {
                model.addAttribute("pcodeName", moduleList.get(0).getName());
            }
        }
        return PREFIX + "menu_edit";
    }

    /**
     * 添加菜单
     */
    @RequestMapping("/addModule")
    @BusinessLog(value = "添加菜单")
    @RequiresPermissions("/module/add")
    @ResponseBody
    public Tip addUser(Module module) {
        String condition = String.format(" code = '%s' ",module.getParentcode());
        List<Module> list = moduleService.getListByCondition(condition);
        Module temp = list.get(0);
        String pc = temp.getParentcodes() + "[" + temp.getCode() + "],";
        module.setParentcodes(pc);

        if(module.getIsmenu() == 1)
        {
            module.setLevels(2);
        }
        else
        {
            module.setLevels(3);
        }
        module.setStatus(1);
        module.setIsopen(1);

        moduleService.insert(module);
        return new Tip();
    }

    /**
     * 修改菜单
     */
    @RequestMapping("/editModule")
    @BusinessLog(value = "修改菜单")
    @RequiresPermissions("/module/edit")
    @ResponseBody
    public Tip editModule(Module module) {
        moduleService.updateById(module);
        return new Tip();
    }

    /**
     * 删除菜单
     */
    @RequestMapping("/remove/{id}")
    @BusinessLog(value = "删除菜单")
    @RequiresPermissions("/module/del")
    @ResponseBody
    public Tip remove(@PathVariable Integer id) {
        moduleService.deleteById(id);
        return new Tip();
    }

    /**
     * 跳转到父级模块的页面
     */
    @RequestMapping("/pCodeTreeView")
    public String pCodeTreeView() {
        return PREFIX + "pCodeTreeView";
    }

    /**
     *获取父级模块列表
     */
    @RequestMapping("/pModuleTree")
    @ResponseBody
    public List<ZTreeNode> pModuleTree() {
        return moduleService.pModuleTree();
    }
}
